//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.server.impl;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.SocketTimeoutException;
import java.util.logging.Logger;

import net.sf.json.JSONObject;
import org.quickserver.net.model.*;
import org.quickserver.net.server.ClientCommandHandler;
import org.quickserver.net.server.ClientEventHandler;
import org.quickserver.net.server.ClientHandler;

public class DefaultClientEventHandler implements ClientEventHandler {
    private static Logger logger = Logger.getLogger(DefaultClientEventHandler.class.getName());
    private ClientCommandHandler clientCommandHandler = null;
    private Method gotConnectedMethod = null;
    private Method lostConnectionMethod = null;
    private Method closingConnectionMethod = null;
    private Method closingDevMethod = null;
    private Method lostDevMethod = null;

    public DefaultClientEventHandler() {
    }

    public void setClientCommandHandler(ClientCommandHandler handler) {
        this.clientCommandHandler = handler;
        if (this.clientCommandHandler != null) {
            this.loadMethods();
        }

    }

    public void gotConnected(ClientHandler handler) throws SocketTimeoutException, IOException {
        if (this.gotConnectedMethod == null) {
            handler.sendSystemMsg("Connection opened: " + handler.getHostAddress());
        } else {
            this.invoke(this.gotConnectedMethod, handler);
        }

    }

    public void lostConnection(ClientHandler handler) throws IOException {
        if (this.lostConnectionMethod == null) {
            handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
        } else {
            this.invoke(this.lostConnectionMethod, handler);
        }

    }

    public void closingConnection(ClientHandler handler) throws IOException {
        if (this.closingConnectionMethod == null) {
            handler.sendSystemMsg("Connection closing: " + handler.getHostAddress());
        } else {
            this.invoke(this.closingConnectionMethod, handler);
        }

    }

    private void loadMethods() {
        Class cls = this.clientCommandHandler.getClass();

        try {
            this.gotConnectedMethod = cls.getMethod("gotConnected", ClientHandler.class);
        } catch (NoSuchMethodException var7) {
            logger.fine("Error finding gotConnected : " + var7);
        }

        try {
            this.lostConnectionMethod = cls.getMethod("lostConnection", ClientHandler.class);
        } catch (NoSuchMethodException var6) {
            logger.fine("Error finding lostConnection : " + var6);
        }

        try {
            this.closingConnectionMethod = cls.getMethod("closingConnection", ClientHandler.class);
        } catch (NoSuchMethodException var5) {
            logger.fine("Error finding lostConnection : " + var5);
        }

        try {
            this.closingDevMethod = cls.getMethod("closingDev", ClientHandler.class, String.class);
        } catch (NoSuchMethodException var4) {
            logger.fine("Error finding lostConnection : " + var4);
        }

        try {
            this.lostDevMethod = cls.getMethod("lostDev", ClientHandler.class, String.class);
        } catch (NoSuchMethodException var3) {
            logger.fine("Error finding lostConnection : " + var3);
        }

    }

    private void invoke(Method method, ClientHandler handler) throws SocketTimeoutException, IOException {
        try {
            method.invoke(this.clientCommandHandler, handler);
        } catch (IllegalAccessException var6) {
            logger.warning("Error invoking " + method + " : " + var6);
        } catch (InvocationTargetException var7) {
            Exception cause = (Exception)var7.getCause();
            if (cause != null) {
                if (SocketTimeoutException.class.isInstance(cause)) {
                    throw (SocketTimeoutException)cause;
                }

                if (IOException.class.isInstance(cause)) {
                    throw (IOException)cause;
                }
            }

            logger.warning("Error invoking " + method + " : " + var7 + "\n Cause: " + cause);
            IOException ioe = new IOException();
            ioe.initCause(cause);
            throw ioe;
        }

    }

    public void closingDev(ClientHandler handler, String uniqueCode) throws IOException {
        GBServerData gbd = (GBServerData)handler.getClientData();
        if (this.closingDevMethod == null) {
            handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
        } else {
            gbd.setDevState("0");

            try {
                this.closingDevMethod.invoke(this.clientCommandHandler, handler, uniqueCode);
            } catch (IllegalAccessException var7) {
                logger.warning("Error invoking " + this.closingDevMethod + " : " + var7);
            } catch (InvocationTargetException var8) {
                Exception cause = (Exception)var8.getCause();
                if (cause != null) {
                    if (SocketTimeoutException.class.isInstance(cause)) {
                        throw (SocketTimeoutException)cause;
                    }

                    if (IOException.class.isInstance(cause)) {
                        throw (IOException)cause;
                    }
                }

                logger.warning("Error invoking " + this.closingDevMethod + " : " + var8 + "\n Cause: " + cause);
                IOException ioe = new IOException();
                ioe.initCause(cause);
                throw ioe;
            }
        }

    }

    public void lostDev(ClientHandler handler, String uniqueCode) throws IOException {
        Object o=handler.getClientData();
        String sbType=(String) JSONObject.fromObject(o).get("sbType");
        if("1".equals(sbType)){
            GBServerData gbd = (GBServerData)handler.getClientData();
            if (this.lostDevMethod == null) {
                handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
            } else {
                gbd.setDevState("0");

                try {
                    this.lostDevMethod.invoke(this.clientCommandHandler, handler, uniqueCode);
                } catch (IllegalAccessException var7) {
                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var7);
                } catch (InvocationTargetException var8) {
                    Exception cause = (Exception)var8.getCause();
                    if (cause != null) {
                        if (SocketTimeoutException.class.isInstance(cause)) {
                            throw (SocketTimeoutException)cause;
                        }

                        if (IOException.class.isInstance(cause)) {
                            throw (IOException)cause;
                        }
                    }

                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var8 + "\n Cause: " + cause);
                    IOException ioe = new IOException();
                    ioe.initCause(cause);
                    throw ioe;
                }
            }
        }else if("2".equals(sbType)){
            GBServerDataS gbd = (GBServerDataS)handler.getClientData();
            if (this.lostDevMethod == null) {
                handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
            } else {
                gbd.setDevState("0");

                try {
                    this.lostDevMethod.invoke(this.clientCommandHandler, handler, uniqueCode);
                } catch (IllegalAccessException var7) {
                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var7);
                } catch (InvocationTargetException var8) {
                    Exception cause = (Exception)var8.getCause();
                    if (cause != null) {
                        if (SocketTimeoutException.class.isInstance(cause)) {
                            throw (SocketTimeoutException)cause;
                        }

                        if (IOException.class.isInstance(cause)) {
                            throw (IOException)cause;
                        }
                    }

                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var8 + "\n Cause: " + cause);
                    IOException ioe = new IOException();
                    ioe.initCause(cause);
                    throw ioe;
                }
            }
        }else if("3".equals(sbType)){
            ZCServerData gbd = (ZCServerData)handler.getClientData();
            if (this.lostDevMethod == null) {
                handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
            } else {
                gbd.setDevState("0");

                try {
                    this.lostDevMethod.invoke(this.clientCommandHandler, handler, uniqueCode);
                } catch (IllegalAccessException var7) {
                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var7);
                } catch (InvocationTargetException var8) {
                    Exception cause = (Exception)var8.getCause();
                    if (cause != null) {
                        if (SocketTimeoutException.class.isInstance(cause)) {
                            throw (SocketTimeoutException)cause;
                        }

                        if (IOException.class.isInstance(cause)) {
                            throw (IOException)cause;
                        }
                    }

                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var8 + "\n Cause: " + cause);
                    IOException ioe = new IOException();
                    ioe.initCause(cause);
                    throw ioe;
                }
            }
        }else if("4".equals(sbType)){
            GBServerDataSZGW gbd = (GBServerDataSZGW)handler.getClientData();
            if (this.lostDevMethod == null) {
                handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
            } else {
                gbd.setDevState("0");

                try {
                    this.lostDevMethod.invoke(this.clientCommandHandler, handler, uniqueCode);
                } catch (IllegalAccessException var7) {
                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var7);
                } catch (InvocationTargetException var8) {
                    Exception cause = (Exception)var8.getCause();
                    if (cause != null) {
                        if (SocketTimeoutException.class.isInstance(cause)) {
                            throw (SocketTimeoutException)cause;
                        }

                        if (IOException.class.isInstance(cause)) {
                            throw (IOException)cause;
                        }
                    }

                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var8 + "\n Cause: " + cause);
                    IOException ioe = new IOException();
                    ioe.initCause(cause);
                    throw ioe;
                }
            }
        }else if("5".equals(sbType)){
            ShengShanServerData gbd = (ShengShanServerData)handler.getClientData();
            if (this.lostDevMethod == null) {
                handler.sendSystemMsg("Connection lost: " + handler.getHostAddress());
            } else {
                gbd.setDevState("0");

                try {
                    this.lostDevMethod.invoke(this.clientCommandHandler, handler, uniqueCode);
                } catch (IllegalAccessException var7) {
                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var7);
                } catch (InvocationTargetException var8) {
                    Exception cause = (Exception)var8.getCause();
                    if (cause != null) {
                        if (SocketTimeoutException.class.isInstance(cause)) {
                            throw (SocketTimeoutException)cause;
                        }

                        if (IOException.class.isInstance(cause)) {
                            throw (IOException)cause;
                        }
                    }

                    logger.warning("Error invoking " + this.lostDevMethod + " : " + var8 + "\n Cause: " + cause);
                    IOException ioe = new IOException();
                    ioe.initCause(cause);
                    throw ioe;
                }
            }
        }


    }
}
